package com.czkt.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.czkt.entity.BasDict;
import com.czkt.entity.SalChance;
import com.czkt.entity.SysUser;
import com.czkt.entity.vo.UserRole;
import com.czkt.service.IBasDictService;
import com.czkt.service.ISalChanceService;
import com.czkt.service.ISysUserService;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2024-06-18
 */
@Controller
public class SalChanceController {
    @Resource
    ISalChanceService salChanceService;

    @Resource
    ISysUserService sysUserService;

    @Resource
    IBasDictService basDictService;

    @RequestMapping("/chance/list")
    public String salChanceList(@RequestParam(value = "custName",defaultValue = "") String custName,
                                @RequestParam(value = "chcDesc",defaultValue = "") String chcDesc,
                                @RequestParam(value = "pageIndex",defaultValue = "1") Integer pageIndex,
                                @RequestParam(defaultValue = "3") Integer pageSize, Model model){
        UserRole userRole = (UserRole) SecurityUtils.getSubject().getPrincipal();
        System.out.println(userRole.getRoleId());
        IPage<SalChance> salChanceIPage = salChanceService.chanceIPage(custName,chcDesc,userRole.getRoleId(),pageIndex,pageSize);
        model.addAttribute("custName",custName);
        model.addAttribute("chcDesc",chcDesc);
        model.addAttribute("chancePage",salChanceIPage);
        return "/chance/list";
    }

    @RequestMapping("/chance/add")
    public String salChanceAdd(Model model){
        model.addAttribute("users",sysUserService.list());
        model.addAttribute("dictAddressList",basDictService.selectByDictType());
        model.addAttribute("dicts",basDictService.selectByDict());
        return "/chance/add";
    }

    @RequestMapping("/chance/insert")
    public String salChanceInsert(SalChance salChance){
        salChanceService.chanceInsert(salChance);
        return "redirect:/chance/list";
    }

    @RequestMapping("/chance/edit")
    public String chanceEdit(String chcId,Model model) throws ParseException {
        SalChance chance = salChanceService.chanceEdit(chcId);
        List<SysUser> sysUser = sysUserService.list();
//        List<BasDict> dicts = basDictService.selectByDict();
//        List<BasDict> AddressDict = basDictService.selectByDictType();
        model.addAttribute("chance",chance);
        model.addAttribute("users",sysUser);
//        model.addAttribute("dicts",dicts);
//        model.addAttribute("dictAddressList",AddressDict);
        return "/chance/edit";
    }

    @RequestMapping("/chance/save")
    public String chanceSave(SalChance salChance){
        salChanceService.toUpdateChance(salChance);
        return "redirect:/chance/list";
    }

    @RequestMapping("/crm/chance/del/{chcId}")
    @ResponseBody
    public Object chanceDel(@PathVariable("chcId") String chcId){
        Map<String,String> map = new HashMap<>();
        salChanceService.deleteByChcId(chcId);
        map.put("delResult","true");
        return map;
    }
}
